#include "threadevent.h"
#include <assert.h>
#include <errno.h>

namespace gecko
{

ThreadEvent::ThreadEvent(bool manualReset /*= false*/)
{
	m_EventHandle = CreateEvent(NULL, manualReset ? TRUE : FALSE, FALSE, NULL);
}

ThreadEvent::~ThreadEvent()
{
	CloseHandle(m_EventHandle);
}

void ThreadEvent::Wait()
{
	WaitForSingleObject(m_EventHandle, INFINITE);
}

bool ThreadEvent::TryWait()
{
	bool signaled = false;

	DWORD result = WaitForSingleObject(m_EventHandle, 0);

	if ( result == WAIT_OBJECT_0 )
	{
		// event was signaled
		signaled = true;
	}

    return signaled;
}

void ThreadEvent::Signal()
{
	SetEvent(m_EventHandle);
}

void ThreadEvent::Reset()
{
	ResetEvent(m_EventHandle);
}

}
